<template>
  <div class="app-container">
    <el-tabs type="border-card">
      <!--修改用户基本信息-->
      <el-tab-pane label="基本资料">
        <person-info />
      </el-tab-pane>

      <!--修改用户头像-->
      <el-tab-pane label="修改头像">
        <el-upload
          ref="upload"
          class="avatar-uploader"
          action="#"
          accept="image/jpeg,image/png,image/gif"
          :limit="1"
          :multiple="false"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="previewAvatarUpload"
          :before-upload="beforeAvatarUpload"
          :on-exceed="handleExceed"
          :http-request="handleUpload"
        >
          <div style="border: 1px solid gray">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" :alt="imageUrl">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </div>
        </el-upload>
        <div class="el-upload__tip">只能上传jpg/png/gif文件，且不超过2MB</div>
        <el-button style="margin-top: 20px;" type="primary" @click="handleSubmit">上传</el-button>
      </el-tab-pane>

      <!--修改用户密码-->
      <el-tab-pane label="密码管理">
        <pass-word />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import PersonInfo from '@/views/account/components/PersonInfo'
import PassWord from '@/views/account/components/PassWord'
import { uploadAvatar } from '@/api/file'

export default {
  name: 'Index',
  components: { PersonInfo, PassWord },
  data() {
    return {
      // 头像上传路径
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false
    }
  },
  methods: {
    // 上传前钩子函数
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isLt2M) {
        this.$notify({
          title: '错误',
          message: '上传头像图片大小不能超过 2MB!',
          type: 'error',
          duration: 2000
        })
      }
      return isLt2M
    },
    // 头像上传处理
    handleUpload(request) {
      console.log(request)
      const formData = new FormData()
      formData.append('file', request.file)
      uploadAvatar(formData).then((res) => {
        // 获取上传头像的路径
        this.$store.commit('user/SET_AVATAR', this.imageUrl)
        this.$refs.upload.clearFiles()
        // 上传成功提示消息
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success',
          duration: 2000
        })
      })
    },
    // 手动上传处理函数
    handleSubmit() {
      this.$refs.upload.submit()
    },
    // 点击文件列表中已上传的文件时的钩子
    previewAvatarUpload(file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 文件超出个数限制时的钩子
    handleExceed(files, fileList) {
      fileList = []
      this.imageUrl = ''
      this.$refs.upload.clearFiles()
      this.$notify.error({
        title: '错误',
        message: '只允许上传一个文件',
        duration: 2000
      })
    }
  }
}
</script>

<!--suppress CssUnusedSymbol -->
<style scoped>
.app-container {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
